package srzy.driver

import srzy.socket._
import actors.Actor
import Actor._
import scala.actors.TIMEOUT

object RBServer {

  def main(args: Array[String]): Unit = {
    deal
  }

  def deal = {

    val dccssc1 = new RBSocketServerConn {
      val serverSocket = SocketServerConn.create(6000)
    }

    //    val dccssc2 = new RBSocketServerConn {
    //      val serverSocket = SocketServerConn.create(8000)
    //    }

    val a1 = Actor.actor {
      loop {
        react {
          case 'exit => exit()
          case 'get => {
            //            while()

            sender ! dccssc1.accept
          }
        }
      }
    }

    //    val a2 = Actor.actor {
    //      loop {
    //        react {
    //          case 'exit => exit()
    //          case 'get => {
    //            //            while()
    //
    //            sender ! dccssc1.accept
    //          }
    //        }
    //      }
    //    }

    def getSocketConn = new RBSocketConn {
      val socket = (a1 !! 'get).apply.asInstanceOf[java.net.Socket]
      println("socket:" + socket)
      val outStream = socket.getOutputStream()
      val inStream = socket.getInputStream()
    }

    val b1 = Actor.actor {

      import srzy.rb._
      var l: srzy.socket.RBSocketConn = getSocketConn
      var i: Int = 1
      var txt: String = ""

      loop {
        try {
          val rece = l.read
          println("rece:" + rece)
          getMsgType(rece) match {
            case 1 => {
              val auth = "[100=02][101=20150407094419069][210=172.168.1.248][003=0000]"
              println("auth:" + auth)
              l.write(auth)
            }
            case _ => {
              val s = i match {
                case 1 => {
                  "[100=81][101=20141105151716794][000=chinaunicom.com;4.1.1;10010][001=2314832620577357824][003=0][R60=000,R601=0,R615=0,R611=0][B10=000,B100=10010,B106=3,B107=102400][B01=000,B010=10010,B015=890026,B016=2,B017=100][B20=000,B201=0,B202=3,B203=1024,B204=1][B21=000,B211=-1,B212=2,B213=9998998][B06=000,B061=0,B062=0,B063=0,B064=0][B07=000,B071=-1,B072=0][B08=0]"
                }
                case 2 => {
                  // Thread.sleep(100000)
                  "[100=81][101=20141105151716828][000=chinaunicom.com;4.1.1;30][001=2316168527205105664][003=0][R60=000,R601=0,R615=0,R611=0][B10=000,B100=30,B106=3,B107=102400][B01=000,B010=30,B015=890057,B016=2,B017=0][B20=000,B201=0,B202=3,B203=1024,B204=0][B21=000,B211=-1,B212=2,B213=9998998][B06=000,B061=0,B062=0,B063=0,B064=0][B07=000,B071=-1,B072=0][B08=0]"
                }
                case 3 => {
                  //                  Thread.sleep(100000)
                  "[100=81][101=20141105151716884][000=chinaunicom.com;4.1.1;10010][001=2316522569949249536][003=0][R60=000,R601=0,R615=0,R611=0][B10=000,B100=10010,B106=3,B107=102400][B03=000,B030=10010,B035=890026,B036=2,B037=200][B01=000,B010=10010,B015=890026,B016=2,B017=100][B20=000,B201=0,B202=3,B203=1024,B204=1][B21=000,B211=-1,B212=2,B213=9998798][B06=000,B061=A60,B062=4,B063=204800,B064=60940288][B07=000,B071=581,B072=200][B08=000,B081=18637151140,B082=2,B083=200,B084=9998898,B085=0,B086=890026]"
                }
                case 4 => {
                  //                  Thread.sleep(100000)
                  "[100=81][101=20141105151716884][000=chinaunicom.com;4.1.1;10010][001=2316522569949249536][003=0][R60=000,R601=0,R615=0,R611=0][B10=000,B100=10010,B106=3,B107=102400][B03=000,B030=10010,B035=890026,B036=2,B037=200][B01=000,B010=10010,B015=890026,B016=2,B017=100][B20=000,B201=0,B202=3,B203=1024,B204=1][B21=000,B211=-1,B212=2,B213=9998798][B06=000,B061=A60,B062=4,B063=204800,B064=60940288][B07=000,B071=581,B072=200][B08=000,B081=18637151140,B082=2,B083=200,B084=9998898,B085=0,B086=890026]"
                }
                //                case 5 => {
                //                  // Thread.sleep(100000)
                //                  "[100=81][101=20141105151716884][000=chinaunicom.com;4.1.1;10010][001=2316522569949249536][003=0][R60=000,R601=0,R615=0,R611=0][B10=000,B100=10010,B106=3,B107=102400][B03=000,B030=10010,B035=890026,B036=2,B037=200][B01=000,B010=10010,B015=890026,B016=2,B017=100][B20=000,B201=0,B202=3,B203=1024,B204=1][B21=000,B211=-1,B212=2,B213=9998798][B06=000,B061=A60,B062=4,B063=204800,B064=60940288][B07=000,B071=581,B072=200][B08=000,B081=18637151140,B082=2,B083=200,B084=9998898,B085=0,B086=890026]"
                //                }
                case 5 => {
                  Thread.sleep(100000)
                  if (getSessionId(rece) == "chinaunicom.com;4.1.1;10010")
                    "[100=81][101=20141105151716884][000=chinaunicom.com;4.1.1;10010][001=2316522569949249536][003=0][R60=000,R601=0,R615=0,R611=0][B10=000,B100=10010,B106=3,B107=102400][B03=000,B030=10010,B035=890026,B036=2,B037=200][B01=000,B010=10010,B015=890026,B016=2,B017=100][B20=000,B201=0,B202=3,B203=1024,B204=1][B21=000,B211=-1,B212=2,B213=9998798][B06=000,B061=A60,B062=4,B063=204800,B064=60940288][B07=000,B071=581,B072=200][B08=000,B081=18637151140,B082=2,B083=200,B084=9998898,B085=0,B086=890026]"
                  else
                    "[100=81][101=20141105151717015][000=chinaunicom.com;4.1.1;30][001=2315755110833061888][003=0][R60=000,R601=0,R615=0,R611=0][B03=000,B030=30,B035=890057,B036=2,B037=0][B20=000,B201=0,B202=3,B203=1024,B204=0][B21=000,B211=-1,B212=2,B213=9998498][B06=000,B061=A60,B062=4,B063=204800,B064=61554688][B07=000,B071=581,B072=0][B08=000,B081=18637151140,B082=2,B083=0,B084=9998498,B085=0,B086=890057]"
                }
                case 6 => {
                  // Thread.sleep(100000)
                  if (getSessionId(rece) == "chinaunicom.com;4.1.1;30")
                    "[100=81][101=20141105151716884][000=chinaunicom.com;4.1.1;10010][001=2316522569949249536][003=0][R60=000,R601=0,R615=0,R611=0][B10=000,B100=10010,B106=3,B107=102400][B03=000,B030=10010,B035=890026,B036=2,B037=200][B01=000,B010=10010,B015=890026,B016=2,B017=100][B20=000,B201=0,B202=3,B203=1024,B204=1][B21=000,B211=-1,B212=2,B213=9998798][B06=000,B061=A60,B062=4,B063=204800,B064=60940288][B07=000,B071=581,B072=200][B08=000,B081=18637151140,B082=2,B083=200,B084=9998898,B085=0,B086=890026]"
                  else
                    "[100=81][101=20141105151717015][000=chinaunicom.com;4.1.1;30][001=2315755110833061888][003=0][R60=000,R601=0,R615=0,R611=0][B03=000,B030=30,B035=890057,B036=2,B037=0][B20=000,B201=0,B202=3,B203=1024,B204=0][B21=000,B211=-1,B212=2,B213=9998498][B06=000,B061=A60,B062=4,B063=204800,B064=61554688][B07=000,B071=581,B072=0][B08=000,B081=18637151140,B082=2,B083=0,B084=9998498,B085=0,B086=890057]"
                }
              }
              println("send:" + s)
              l.write(s)
              i = i + 1
            }
          }

        } catch {
          case sce: SocketClosedException => {
            l.close
            l = getSocketConn
          }
          case ex: Exception => {
            l.close
            ex.printStackTrace()
            println("socket error ,reconnect start")
            l = getSocketConn
          }
        }
      }

    }

  }

}